//
//  CWQ_historyLearnView.m
//  BaxTer
//
//  Created by 陈武琦 on 16/9/22.
//  Copyright © 2016年 陈武琦. All rights reserved.
//


#import "CWQ_imageLabel.h"
#import "Masonry.h"
#define itemSpace kScreenWidth/16
@interface CWQ_historyLearnFirstLineCell: UICollectionViewCell
@property (strong, nonatomic) CWQ_imageLabel *imageLabel;
@property(nonatomic, strong)id model;
@end

@implementation CWQ_historyLearnFirstLineCell
static NSString *cellID = @"CWQ_historyLearnFirstLineCellID";

-(CWQ_imageLabel *)imageLabel {
    if (_imageLabel) return _imageLabel;
    _imageLabel = [CWQ_imageLabel imageLabel];
    [self.contentView addSubview:_imageLabel];
    [_imageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.contentView).with.offset(0);
    }];
    _imageLabel.userInteractionEnabled = NO;
    return _imageLabel;
}
- (void)setModel:(id)model {
    _model = model;
    self.imageLabel.model = model;
}

@end


#import "CWQ_historyLearnView.h"

@implementation CWQ_historyLearnView

- (void)setModels:(NSArray *)models {
    _models = [models copy];
    [self.collectionView reloadData];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    self.userInteractionEnabled = YES;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 自定义的布局对象
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.minimumLineSpacing = itemSpace;
    [_collectionView setCollectionViewLayout:layout];
    _collectionView.backgroundColor = kAppBg;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;

    [_collectionView registerClass:[CWQ_historyLearnFirstLineCell class] forCellWithReuseIdentifier:cellID];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;

//    _collectionView.pagingEnabled = YES;
}

#pragma mark UICollectionViewDataSource

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat w = kScreenWidth/4;
    CGFloat h = self.frame.size.height - 10;
    return CGSizeMake(w, h);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.models.count;

}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//    WSJLog(@"__________-__%d",section);
//    return itemSpace;
//
//}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, itemSpace, 5, itemSpace);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CWQ_historyLearnFirstLineCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[CWQ_historyLearnFirstLineCell alloc] init];
    }
    [cell setModel:self.models[indexPath.row]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_delegate respondsToSelector:@selector(learnView:didSelect:)]) {
        [_delegate learnView:self didSelect:self.models[indexPath.row]];
    }
}


@end
